<template>
  <div class="articleDetails">
    <div class="nav-top">
      <van-nav-bar
        title="黑马头条"
        left-text="返回"
        left-arrow
        @click-left="$router.back()"
      />
    </div>

    <div class="content">
      <!-- 加载中 -->
      <div class="loading-wrap">
        <van-loading color="#3296fa" vertical>加载中</van-loading>
      </div>
      <!-- /加载中 -->

      <div class="main-wrap">
        <!-- 加载完成-文章详情 -->
        <div class="article-detail">
          <!-- 文章标题 -->
          <h1 class="article-title">{{ articleObj.title }}</h1>
          <!-- /文章标题 -->

          <!-- 用户信息 -->
          <van-cell class="user-info" center :border="false">
            <van-image
              class="avatar"
              slot="icon"
              round
              fit="cover"
              :src="articleObj.aut_photo"
            />
            <div slot="title" class="user-name">{{ articleObj.aut_name }}</div>
            <div slot="label" class="publish-date">
              {{ articleObj.pubdate | relativeTime }}
            </div>
            <van-button :loading="loading" type="info" @click="guanzhu">{{
              fllow
            }}</van-button>
          </van-cell>
          <!-- /用户信息 -->

          <!-- 文章内容 -->
          <div
            class="article-content"
            ref="article-content"
            v-html="articleObj.content"
          ></div>
          <van-divider>正文结束</van-divider>
        </div>
        <!-- /加载完成-文章详情 -->

        <!-- 文章评论列表 -->
        <comments-list :target="articleid" :list="list" @replycomment='replycomment'></comments-list>
        <!-- 文章评论列表 -->
      </div>

      <!-- 图片预览组件 -->
      <van-image-preview v-model="show" :images="images"> </van-image-preview>
      <!-- 图片预览组件 -->

      <!-- 底部内容 -->
      <van-cell class="footer-content">
        <template slot="title">
          <van-button
            @click="showInput = true"
            class="comments-btn"
            round
            type="info"
            size="mini"
            >写评论</van-button
          >
        </template>

        <template slot="extra">
          <div class="comments-icon">
            <van-icon name="comment-o" badge="9" />
            <collection :articleObj="articleObj"></collection>

            <praise :articleObj="articleObj"></praise>

            <van-icon name="share" />
          </div>
        </template>
      </van-cell>
      <!-- 底部内容 -->

      <!-- 文章评论弹出层 -->
      <van-popup v-model="showInput" round position="bottom">
        <comment-post
          :target="articleid"
          @commentVlue="commentVlue"
        ></comment-post>
      </van-popup>
      <!-- 文章评论弹出层 -->

      <!-- 评论的评论弹出 -->
      <van-popup
        class="comment-replay"
        v-model="show2"
        position="bottom"
        :style="{ height: '100%' }"
        >
        <comment-reply :replyDate='replyDate' @closeShow='closeShow'></comment-reply>
        </van-popup
      >
      <!-- 评论的评论弹出 -->
    </div>
  </div>
</template>

<script>
import { articlesDetails } from "@/api/articles";

import { ImagePreview } from "vant";

import collection from "./components/collection";

import praise from "./components/praise";

import commentPost from "./components/comment-post";

import commentsList from "./components/comment-list";

import commentReply from './components/comment-reply'

export default {
  components: {
    collection,
    praise,
    commentPost,
    commentsList,
    commentReply
  },
  data() {
    return {
      articleid: "",
      articleObj: {},
      loading: false,
      fllow: "+关注",
      images: [],
      show: false,
      index: 0,

      showInput: false, // 文章评论弹出

      list: [], // 文章评论内容

      show2 : false,  // 控制评论的评论弹出层
      replyDate : {}   // 当前评论的哪一项
    };
  },
  methods: {
    async getArticle() {
      const { data: res } = await articlesDetails(this.articleid);
      console.log(res, 333);
      this.articleObj = res.data;
      setTimeout(() => {
        this.getImgNode();
      }, 0);
    },
    guanzhu() {
      if (this.fllow == "+关注") {
        this.loading = true;
        setTimeout(() => {
          this.loading = false;
          this.fllow = "已关注";
        }, 1000);
      } else {
        this.loading = true;
        setTimeout(() => {
          this.loading = false;
          this.fllow = "+关注";
        }, 1000);
      }
    },

    //获取图片
    getImgNode() {
      console.log(
        this.$refs["article-content"].querySelectorAll("img"),
        444444
      );
      const imgArr = this.$refs["article-content"].querySelectorAll("img");

      imgArr.forEach((item, index) => {
        this.images.push(item.src);
        item.onclick = () => {
          this.show = !this.show;
          console.log(index, 111);

          ImagePreview({
            images: this.images,
            startPosition: index,
          });
          this.show = false;
        };
      });
    },
    // 获取文章评论
    commentVlue(res) {
      console.log(res, 88888);
      this.showInput = false;
      this.list.unshift(res);
      console.log(this.list, 777777);
    },


    // 控制评论的评论和评论的弹出层

    replycomment(data){
        this.show2 = true
        console.log(data,6789);
        this.replyDate = data
    },

    //关闭评论弹窗
    closeShow(){
      this.show2 = false
    }
  },
  created() {
    console.log(this.$route.params.id);
    this.articleid = this.$route.params.id;

    this.getArticle();
  },
};
</script>

<style scoped  lang='less'>
@import "./github-markdown.css";
.van-nav-bar {
  background-color: #1989fa;
  /deep/.van-nav-bar__title {
    color: ivory;
  }
  /deep/.van-icon {
    color: #fff;
  }
}
.main-wrap {
  width: 100%;
  position: fixed;
  left: 0;
  right: 0;
  top: 92px;
  bottom: 88px;
  overflow-y: scroll;
  background-color: #fff;
  overflow-x: auto;
  .article-detail {
    .article-title {
      font-size: 40px;
      padding: 50px 32px;
      margin: 0;
      color: #3a3a3a;
      font-weight: 600;
    }
    .user-info {
      padding: 0 32px;
      .avatar {
        width: 100px;
        height: 100px;
        margin-right: 17px;
      }
      .van-cell__label {
        margin-top: 0;
      }
      .user-name {
        font-size: 24px;
        color: #3a3a3a;
      }
      .publish-date {
        font-size: 23px;
        color: #b7b7b7;
      }
      .follow-btn {
        width: 170px;
        height: 58px;
      }
    }
    .article-content {
      padding: 55px 32px;
      font-size: 40px;
      line-height: 60px;
      /deep/ p {
        word-wrap: break-word;
        text-align: justify;
        margin: 20px 0;
      }
    }
  }
}
/deep/a {
  width: 100vh;
}
/deep/ img {
  width: 100%;
}

.footer-content {
  position: fixed;
  bottom: 0;
}
/deep/.comments-btn {
  width: 260px;
  margin-right: 40px;
}
/deep/.comments-icon {
  flex: 3;
  display: flex;
  justify-content: space-around;
  align-items: center;
  font-size: 40px;
}
</style>